﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iMapa.Backend;
using System.Windows.Media.Imaging;
using System.Windows;
using System.ComponentModel;
using iMapa.UI.ModelView.Common;
using iMapa.Helper;
using Emgu.CV;
using Emgu.CV.Structure;

namespace iMapa.UI.ModelView.Main.Wizard
{
    public class NewIMapWizardViewModel:ViewModelBase
    {
        public enum SourceSelector { opt0,opt1,opt2,opt3}

        private SelectionAreaViewModel selector;
        private SourceSelector selected= SourceSelector.opt0;
        private BitmapSource bms;
        private BitmapSource clipimage;
        private string filename;
        private SimulationGroupsViewModel mgmv;
        private BitmapSource finalImage;
        private int selectedGroupIndex;
        private string mapName;

        public NewIMapWizardViewModel(SimulationGroupsViewModel mgmv)
        {
            selector = new SelectionAreaViewModel(500, 500,10);
            CameraInput.getInstance();
            this.mgmv = mgmv;
        }


        public int SelectedGroupIndex
        {
            get { return selectedGroupIndex; }
            set { selectedGroupIndex = value;
            onPropertyChanged("SelectedGroupIndex");
            }
        }

        public BitmapSource FinalImage
        {
            get { return finalImage; }
            set { finalImage = value;
            onPropertyChanged("Image");
            }
        }

        public SelectionAreaViewModel Selector
        {
            get { return selector; }
            set { selector = value; }
        }

        public SourceSelector Selected
        {
            get { return selected; }
            set {if(value!=SourceSelector.opt0) 
                selected = value; }
        }

        public BitmapSource Image
        {
            get {
                if (FinalImage != null)
                {
                    return FinalImage;
                }
                switch (Selected)
                {
                    case SourceSelector.opt1:
                        return Bitmap;
                    case SourceSelector.opt2:
                        return getImageFromFilename(Filename);
                    case SourceSelector.opt3:
                        return Clipimage;
                    case SourceSelector.opt0:
                        return null;
                }
                return null;
            }
        }

        private BitmapSource getImageFromFilename(string Filename)
        {
            try
            {
                BitmapSource s = new BitmapImage(new Uri(Filename));
                return s;
            }
            catch
            {
                return null;
            }
        }

        public string Filename
        {
            get { return filename; }
            set { filename = value;
            onPropertyChanged("FileImageVisibility");
            }
        }

        public SimulationGroupsViewModel ParentContext
        {
            get { return mgmv; }
            set { mgmv = value; }
        }

        public BitmapSource Clipimage
        {
            get { return clipimage; }
            set { clipimage = value;
            onPropertyChanged("Clipimage");
            onPropertyChanged("ClipboardError");   
            }
        }


        public BitmapSource Bitmap
        {
            get { return bms; }
            set { bms = value; }
        }

        public string MapName
        {
            get { return mapName; }
            set { mapName = value; }
        }

        public Visibility IsCameraSelectorVisible
        {
            get{
                return CameraInput.getInstance().getConnectedCamCount()>1? Visibility.Visible:Visibility.Collapsed ;
            }
        }

        public Visibility IsAnyCameraAvailaible
        {
            get
            {
                return CameraInput.getInstance().getConnectedCamCount() != 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility NoCameraConnected
        {
            get
            {
                return CameraInput.getInstance().getConnectedCamCount() == 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }


        public Visibility ClipboardError
        {
            get
            {
                return clipimage == null ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility FileImageVisibility
        {
            get
            {
                return (filename == null || "".Equals(filename)) ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        public void RestartCameraCapture()
        {
            CameraInput.RestartCapture();
            onPropertyChanged("IsCameraSelectorVisible");
            onPropertyChanged("IsAnyCameraAvailaible");
            onPropertyChanged("NoCameraConnected");
        }

        public void ReadClipboard()
        {
            try
            {
                if (System.Windows.Forms.Clipboard.ContainsImage())
                {
                    Image<Bgr,byte> img = new Image<Bgr,byte>((System.Drawing.Bitmap)
                        System.Windows.Forms.Clipboard.GetImage());
                    Clipimage = Utils.ImgUtils.ToBitmapSource(img);
                }
                else
                {
                    clipimage = null;
                }

            }
            catch {
                clipimage = null;
            }
        }

        internal bool ValidateStep()
        {
            if (Image == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
